package cn.edu.Day05;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class MyPanel extends JPanel implements Runnable{
    int x=200,y=50;
//    int xs[];
//    int ys[];
    Star stars[];
    Random ran;
    MyPanel()
    {
        //星星数组的赋值
        ran=new Random();
        stars=new Star[300];
//        xs=new int[300];
//        ys=new int[300];
        for(int i=0;i<stars.length;i++)
        {
//            xs[i]=ran.nextInt(MyFrame.W);
//            ys[i]=ran.nextInt(MyFrame.H);
            stars[i]=new Star();
        }
        new Thread(this).start();
    }
    public void paint(Graphics g)
    {
        super.paint(g);
        this.setBackground(Color.BLACK);
        g.setColor(Color.YELLOW);
       // g.drawLine(0,0,100,100);
        g.fillOval(230,100,100,100);
        g.setColor(Color.BLACK);
        g.fillOval(x,y,100,100);
        for(int i=0;i<stars.length;i++)
        {
            g.setColor(stars[i].getColor());
            g.setFont(stars[i].getFont());
            //g.setColor(new Color(ran.nextInt(256),ran.nextInt(256),ran.nextInt(256)));
            g.drawString("*",stars[i].getX(),stars[i].getY());
        }

    }
    public void run()
    {
        while(true)
        {
            x++;
            y++;
            repaint();
            //实现星星的下落
            for (int i = 0; i < stars.length; i++) {
                stars[i].setY(stars[i].getSpeed()+5);
//                ys[i]+=5;
                if(stars[i].getY()>MyFrame.H)
                {
                    stars[i].setY(0);
                }

            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
